package Solution_2;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class Chess {
    private char [][] chessboard = new char[17][17] ;
    private final char BLACK = '@';
    private final char WHITE = '#';
    private final boolean isBlack = true;
    private final boolean isWhite = false;
    private int mod = 0;
    public Chess() {

        for (int i = 1; i < 17; i++) {

            for (int j = 1; j < 17 ; j++) {
                chessboard[i][j] = '+';
            }
        }
    }

    /**
     * 游戏开始
     */
    public void playGame() {
        //Scanner in = new Scanner(System.in);
        System.out.println("欢迎进入五子棋游戏，规则：\n只要某方先5个棋子直线能联通就赢\n黑棋先下");
        boolean who = isBlack;
        while (put(who)) who = !who;
        System.out.println("游戏结束！！");
    }

    /**
     * 检查是不是赢了
     * @param who
     * @return
     */
    private boolean testWin(boolean who,int x,int y){
        char chess = who ? BLACK : WHITE;
        //1:横 2：纵 3斜下 4：斜上
        for(int i = 1;i<5;i++){
            if(judge(x,y,chess,i) == 4) return true;
        }
        return false;
    }


    private int judge(int x , int y ,char chess,int position) {
        int count  = 0;
        switch (position){
            case 1:{
                int y1 = y,cnt =0;
                while (cnt < 5 && chessboard[x][--y1] == chess && y1>0) {
                    cnt++;
                }

                    y1 = y;
                    while (cnt < 5 && chessboard[x][++y1] == chess && y1<=16){
                        cnt++;
                    }
                    count = cnt;


            }break;

            case 2:{
                int x1 = x,cnt =0;
                while (cnt < 5 && chessboard[--x1][y] == chess && x1>0) {
                    cnt++;
                }

                    x1 = x;
                    while (cnt < 5 && chessboard[++x1][y] == chess && x1<=16){
                        cnt++;
                    }
                    count = cnt;

            }break;

            case 3:{
                int x1 = x,y1=y,cnt =0;
                while (cnt < 5 && chessboard[--x1][--y1] == chess && x1>0&&y1>0) {
                    cnt++;
                }

                x1 = x;y1=y;
                while (cnt < 5 && chessboard[++x1][++y1] == chess && x1<=16 && y1<=16){
                    cnt++;
                }
                count = cnt;
            }break;

            case 4:{
                int x1 = x,y1=y,cnt =0;
                while (cnt < 5 && chessboard[--x1][++y1] == chess && x1>0&&y1<=16) {
                    cnt++;
                }

                x1 = x;y1=y;
                while (cnt < 5 && chessboard[++x1][--y1] == chess && x1<=16 && y1>0){
                    cnt++;
                }
                count = cnt;
            }break;

        }

        return count;
    }

    /**
     * 判断谁在下棋
     * @param who
     * @return
     */
    private boolean put(boolean who){
        //重定向输入 方便演示数据
       /* FileInputStream f = null;
        try {
            f = new FileInputStream("src/Solution_2/data.in");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        System.setIn(f);*/
        Scanner in = new Scanner(System.in);
        int x ,y;
        String whoPut = who==isBlack ? "黑棋" : "白棋";
        System.out.println("现在是 "+ whoPut +" 在下,请输入棋子放置位置：");
        //落子
        while (!putChess(who,x=in.nextInt(),y=in.nextInt())){
           // System.out.println(x + "   " + y);
            System.out.println("该位置已经被放置 或 超出棋盘 请重下");
        }

        mod++;
        printChessboard();
        if(mod >= 9 && testWin(who,x,y)) {

            System.out.println("恭喜 " + whoPut+" 赢了!!");
            return false;
        }
        return true;
    }

    /**
     * 落子 黑棋 ‘@’ 白棋 ‘#’
     * @param who
     * @param x
     * @param y
     */
    private boolean putChess(boolean who,int x,int y) {
        if(x<=0 || x>16 || y<=0 || y>16 ) return false;
        char chess = who ? BLACK : WHITE;
        if( chessboard[x][y] == '+') {
            chessboard[x][y] = chess;
            return true;
        }
        return false;
    }

    /**
     * 打印棋盘
     */
    private void printChessboard() {
        for (int i = 0; i < 17; i++) {

            for (int j = 0; j < 17 ; j++) {

                if( i == 0 && j == 0) {
                    System.out.print("   ");
                    continue;
                }

                if(i > 0 && j > 0) System.out.print(chessboard[i][j]+"  ");
                else if (i == 0){

                    System.out.print(((j-1) <= 9 ? (char) ((j-1)+'0') : (char)('a'+j-1-10) ) + "  ");
                }
                else System.out.print(((i-1) <= 9 ? (char) ((i-1)+'0') : (char)('a'+i-1-10) ) + "  ");
            }

            System.out.println();
        }
    }

    public static void main(String[] args) {
         new Chess().playGame();
    }
}
